<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" :model="queryForm" class="demo-form-inline">
        <el-form-item label="事业群">
          <el-select
              filterable
              clearable
              v-model="queryForm.groupUuid"
              placeholder="请选择事业群">
            <el-option
                v-for="item in groupOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目组名称">
          <el-input v-model="queryForm.teamName" placeholder="模糊查询"></el-input>
        </el-form-item>
        <el-form-item label="项目组ID">
          <el-input v-model="queryForm.teamId" placeholder="模糊查询"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" @click="addNew">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          :data="pageResult.data"
          border
          :default-sort="{prop: 'updateTime', order: 'descending'}"
          style="width: 100%; margin-top: 20px;font-size: 10px">
        <el-table-column
            align="center"
            label="事业群">
          <template slot-scope="scope">
            {{ getGroupName(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            prop="teamName"
            label="项目组名称">
        </el-table-column>
        <el-table-column
            align="center"
            prop="teamId"
            label="项目组ID">
        </el-table-column>
        <el-table-column
            align="center"
            prop="desc"
            label="描述">
        </el-table-column>
        <el-table-column
            align="center"
            label="是否有效">
          <template slot-scope="scope">
            <span v-if="scope.row.isDelete === 1">无效</span>
            <span v-if="scope.row.isDelete === 0">有效</span>
          </template>
        </el-table-column>
        <el-table-column
            sortable
            align="center"
            width="300"
            label="创建/修改时间">
          <template slot-scope="scope">
            {{ ddkDateFormat(scope.row.createTime) }} / {{ ddkDateFormat(scope.row.updateTime) }}
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            width="100"
            label="状态">
          <template slot-scope="scope">
            <span style="color: red" v-if="scope.row.isDelete === 1"><strong>离线</strong></span>
            <span style="color: #189c1a" v-if="scope.row.isDelete === 0"><strong>在线</strong></span>
          </template>
        </el-table-column>
        <el-table-column
            fixed="right"
            align="center"
            width="200"
            label="操作">
          <template slot-scope="scope">
            <el-button style="margin-left: 0px" type="text" size="mini" @click="toEdit(scope.row)">编辑</el-button>
            <el-button style="margin-left: 0px;color: #189c1a" type="text" size="mini" @click="toDisableOrEnable(scope.row)">
              <span v-if="scope.row.isDelete === 1">启用</span>
              <span style="color: red" v-if="scope.row.isDelete === 0">禁用</span>
            </el-button>
            <!-- TODO 暂时不支持删除 -->
            <!--            <el-button style="margin-left: 0px;color: red" type="text" size="mini" @click="deleteOne(scope.row)">删除</el-button>-->
            <vue-log module="DDK_TEAM" :target-id="scope.row.uuid"></vue-log>
          </template>
        </el-table-column>
      </el-table>

      <el-footer>
        <vue-page :paginator="pageResult.paginator" :query="query"></vue-page>
      </el-footer>
    </el-card>
    <team-form ref="teamForm"></team-form>
  </div>
</template>

<script>

import teamForm from "./dialog/teamForm";
import vuePage from "../common/vue-page.vue"
import vueLog from "../common/vue-log.vue"
import {loadGroupSelector} from "../../api/loadSelectorApi";
import {teamQuery} from "../../api/teamApi";

export default {
  name: "group",
  components: {
    teamForm,
    vuePage,
    vueLog
  },
  data() {
    return {
      queryForm: {
        teamId: '',
        teamName: '',
        groupUuid: '',
      },
      groupOptions: [],
      // 查询返回结果
      pageResult: {
        data: [],
        paginator: {
          currentPage: 0,
          totalCount: 0,
          pageSize: 10,
          totalPage: 0
        }
      },
      logTargetId: undefined,
    }
  },
  created() {
    this.query()
  },
  methods: {
    async query() {
      let vm = this
      vm.groupOptions = await loadGroupSelector()
      let ret = await teamQuery(vm.queryForm, vm.pageResult.paginator)
      if (ret.ok) {
        vm.pageResult = ret.data.queryPage
      } else {
        vm.$message({type: 'error', message: '查询失败：' + ret.message});
      }
    },
    addNew() {
      this.$refs.teamForm.load();
    },
    toEdit(row) {
      this.$refs.teamForm.load(row);
    },
    getGroupName(row) {
      let vm = this
      for (let i = 0; i < vm.groupOptions.length; i++) {
        let groupOption = vm.groupOptions[i];
        if (groupOption.value === row.groupUuid) {
          return groupOption.label
        }
      }
      return row.groupUuid;
    },
    /**
     * TODO 下线需要进行验证，暂时没做
     *
     * @param row
     */
    toDisableOrEnable(row) {
      let vm = this
      let clone = vm.$cloneObj(row);
      clone.isDelete = clone.isDelete ^ 1
      vm.$confirm('是否确认修改为【' + (clone.isDelete === 1 ? '无效' : '有效') + '】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$refs.teamForm.doSave(clone);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    /**
     * TODO 删除的逻辑比较复杂，暂不处理
     *
     * @param row row
     */
    deleteOne(row) {

    }
  }
}
</script>

<style scoped>

</style>
